package com.fire.control.system.entity;

import java.util.Collection;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonManagedReference;


@Entity
@Table(name = "t_users") 
public class User extends BaseEntity{
	
	
	private static final long serialVersionUID = -5868008856414966497L;

	@Column(name = "login_name", nullable = false,length=50)
	private String loginName;
	
	@Column(name = "password", nullable = false)
	private String password;
	
	@Column(name = "name", nullable = false,length=10)
	private String name;
	
	@Column(name = "tel_number", nullable = false,length=20)
	private String telNumber;
	
	@Column(name = "email",length=50)
	private String email;
	
	@Column(name = "change_pwd")
	private boolean changePwd;
	
	@Column(name = "user_type",nullable = true,length=15)
	//@Enumerated(EnumType.STRING)
	private String userType;
	
	@Column(name = "address",nullable = true,length=255)
	private String address;
	
	@Column(name = "user_icon",nullable = true,length=50)
	private String userIcon;
	
	@Column(name = "parent_id",nullable = true,length=50)
	private String parentid;
	
	@Column(name = "sex",nullable = true,length=50)
	private String sex;
	
	@Column(name = "enable",nullable = true,length=50)
	private boolean enable;
	
	@Column(name = "province_id",length=50,nullable = true)
	private String provinceId;
	
	@Column(name = "city_id",length=50,nullable = true)
	private String cityId;
	
	@Column(name = "area_id",length=50,nullable = true)
	private String areaId;
	
	@JsonManagedReference
	@ManyToMany(cascade={CascadeType.REFRESH,CascadeType.MERGE},fetch = FetchType.LAZY)
	@JoinTable(name="t_user_role",
		joinColumns=@JoinColumn(name="user_id"),
		inverseJoinColumns=@JoinColumn(name="role_id"))
	private Collection<Role> role;
	
	@JsonManagedReference
	@ManyToMany
	@JoinTable(name="t_user_device_group",
		joinColumns=@JoinColumn(name="user_id"),
		inverseJoinColumns=@JoinColumn(name="group_id"))
	private Collection<DeviceGroup> deviceGroups;
	
	@JsonManagedReference
	@ManyToOne(cascade={CascadeType.REFRESH},fetch = FetchType.EAGER,optional=false) 
	@JoinColumn(name="company_id") 
	private Company company;

	/*@JsonManagedReference
	@ManyToOne(cascade={CascadeType.ALL},fetch = FetchType.EAGER,optional=false) 
	@JoinColumn(name="area_id") 
	private Area area;*/
	
	
	
	public Company getCompany() {
		return company;
	}

	public boolean isChangePwd() {
		return changePwd;
	}

	public void setChangePwd(boolean changePwd) {
		this.changePwd = changePwd;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public Collection<DeviceGroup> getDeviceGroups() {
		return deviceGroups;
	}

	public void setDeviceGroups(Collection<DeviceGroup> deviceGroups) {
		this.deviceGroups = deviceGroups;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTelNumber() {
		return telNumber;
	}

	public void setTelNumber(String telNumber) {
		this.telNumber = telNumber;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	
	public void setUser(User u){
		this.address = u.getAddress();
		this.company = u.getCompany();
		this.email = u.getEmail();
		this.loginName = u.getLoginName();
		this.name = u.getName();
		this.password = u.getPassword();
		this.telNumber = u.getTelNumber();
		this.userType = u.getUserType();
		this.company = u.getCompany();
		this.provinceId = u.getProvinceId();
		this.cityId = u.getCityId();
		this.areaId = u.getAreaId();
		this.setStatus(u.getStatus());
		this.setEnable(u.isEnable());
	}
	
	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public User() {
		// TODO Auto-generated constructor stub
	}
	
	public User(String id) {
		this.setId(id);
	}


	public String getUserIcon() {
		return userIcon;
	}

	public void setUserIcon(String userIcon) {
		this.userIcon = userIcon;
	}

	/*public Area getArea() {
		return area;
	}

	public void setArea(Area area) {
		this.area = area;
	}*/

	public Collection<Role> getRole() {
		return role;
	}

	public void setRole(Collection<Role> role) {
		this.role = role;
	}

	public String getParentid() {
		return parentid;
	}

	public void setParentid(String parentid) {
		this.parentid = parentid;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public boolean isEnable() {
		return enable;
	}

	public void setEnable(boolean enable) {
		this.enable = enable;
	}

	public String getProvinceId() {
		return provinceId;
	}

	public void setProvinceId(String provinceId) {
		this.provinceId = provinceId;
	}

	public String getCityId() {
		return cityId;
	}

	public void setCityId(String cityId) {
		this.cityId = cityId;
	}

	public String getAreaId() {
		return areaId;
	}

	public void setAreaId(String areaId) {
		this.areaId = areaId;
	}




/*	public Area getArea() {
		return area;
	}

	public void setArea(Area area) {
		this.area = area;
	}*/

}
